package com.um.jdy.app.shop.activity.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.NumberUtil;
import com.um.jdy.app.activity.entity.vo.GiftGoodsOptionVO;
import com.um.jdy.app.shop.activity.Activity;
import com.um.jdy.app.shop.activity.BaseActivity;
import com.um.jdy.app.shop.entity.bo.*;
import com.um.jdy.common.activity.code.CouponsCode;
import com.um.jdy.common.activity.entity.po.Coupons;
import com.um.jdy.common.activity.service.CouponsService;
import com.um.jdy.common.base.enums.YesOrNo;
import com.um.jdy.common.market.entity.po.FullReduce;
import com.um.jdy.common.market.entity.po.FullReduceGoods;
import com.um.jdy.common.market.service.FullReduceGoodsService;
import com.um.jdy.common.market.service.FullReduceService;
import com.um.jdy.common.shop.code.ShopGoodsCode;
import com.um.jdy.common.shop.enums.ActivityEnum;
import com.um.jdy.common.shop.enums.CouponsEnum;
import com.um.springboot.starter.exception.ServiceException;
import com.um.springboot.starter.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 满减活动
 * @Title
 * @Description
 * @Author renjunwei
 * @Date 2021/6/28 4:34 下午
 */
@Service
@Transactional
public class FullReduceActivityImpl extends BaseActivity implements Activity {

    @Autowired
    FullReduceService fullReduceService;

    @Autowired
    CouponsService couponsService;

    @Autowired
    FullReduceGoodsService fullReduceGoodsService;

    /**
     * 保存活动对象
     *
     * @param activityBO
     */
    @Override
    public void setActivityEntity(ActivityBO activityBO) {
        this.activityBO = activityBO;
    }

    /**
     * 获取活动实体
     *
     * @return
     */
    @Override
    public ActivityBO getActivityEntity() {
        return this.activityBO;
    }

    /**
     * 是否满足活动条件
     *
     * @return
     */
    @Override
    public boolean isMeet(List<GoodsBO> goodsBOList) {
        boolean isMeet = false;
        List<GoodsBO> activityGoodsList = new ArrayList();//参与活动的订单商品
        Integer totalGoodsPrice = 0;
        Integer fee = 0;

        //参与满减商品列表
        List<FullReduceGoods> currentActivityGoodsList = fullReduceGoodsService.getListBySqlName("get_full_reduce_goods_list",new Object[]{this.activityBO.getMarketActivityId(),fullReduceGoodsService.getComId()});
        for(GoodsBO goodsBO : goodsBOList){
            boolean goodsExist = currentActivityGoodsList.stream().filter(item -> item.getShopGoodsId().equals(goodsBO.getShopGoodsId())).collect(Collectors.toList()).size() > 0;
            if(goodsExist){
                totalGoodsPrice = NumberUtil.add(totalGoodsPrice, goodsBO.getTotalPrice()).intValue();
                activityGoodsList.add(goodsBO);
            }
        }

        //满减门槛
        List<FullReduce> fullReduceList = fullReduceService.getList(
                new String[]{"market_activity_id"},
                new String[]{this.activityBO.getMarketActivityId()},
                new String[]{"full_price"},
                new String[]{"desc"});
        for (FullReduce fullReduce : fullReduceList) {

            //达到门槛
            if(totalGoodsPrice >= StringUtil.toDouble(fullReduce.getFullPrice())){
                isMeet = true;

                for(GoodsBO goodsBO : activityGoodsList){ //对象引用
                    goodsBO.setMarketActivityId(this.activityBO.getMarketActivityId());
                    goodsBO.setActivityType(this.activityBO.getType());
                    Integer activityPrice = new Integer(goodsBO.getPrice());
                    Integer totalPrice = new Integer(goodsBO.getTotalPrice());

                    //计算优惠
                    switch (ActivityEnum.FullReduceType.valueOf(fullReduce.getType())){
                        case Money:
                            Integer reducePrice = NumberUtil.mul(NumberUtil.div(goodsBO.getPrice(),totalGoodsPrice,2), fullReduce.getPrice().doubleValue()).intValue();
                            activityPrice = NumberUtil.sub(goodsBO.getPrice(), reducePrice).intValue();
                            Integer reduceTotalPrice = NumberUtil.mul(NumberUtil.div(goodsBO.getTotalPrice(),totalGoodsPrice,2), fullReduce.getPrice().doubleValue()).intValue();
                            totalPrice = NumberUtil.sub(goodsBO.getTotalPrice(), reduceTotalPrice).intValue();
                            goodsBO.setActivityTitle("满"+fullReduce.getFullPrice()+"减"+fullReduce.getPrice());
                            break;
                        case Discount:
                            activityPrice = (int) NumberUtil.mul(goodsBO.getPrice().doubleValue(),NumberUtil.div(fullReduce.getScale().intValue(),10));
                            totalPrice = (int) NumberUtil.mul(goodsBO.getNum().doubleValue(),activityPrice.doubleValue());
                            goodsBO.setActivityTitle("满"+fullReduce.getFullPrice()+"打"+NumberUtil.div(fullReduce.getScale().doubleValue(), 10)+"折");
                            break;
                        case Coupon:
                            Coupons coupons = couponsService.getEntity(fullReduce.getActivityCouponsId());
                            if(null == coupons){
                                throw new ServiceException(CouponsCode.CouponsNotExist);
                            }
                            CouponsBO couponsBO = BeanUtil.toBean(coupons, CouponsBO.class);
                            couponsBO.setActivityTitle("满"+fullReduce.getFullPrice()+"赠"+ CouponsEnum.Type.valueOf(coupons.getType()).getValue());
                            //获取赠品列表
                            List<CouponsGiftGoodsBO> couponsGiftGoodsBOList = new ArrayList();

                            if(CouponsEnum.Type.Gift.name().equals(coupons.getType())){
                                List<GiftGoodsOptionVO> giftGoodsList = couponsService.getViewModelList("get_gift_goods_and_option_list", GiftGoodsOptionVO.class,
                                        new Object[]{coupons.getActivityCouponsId(),coupons.getComId()});
                                for (GiftGoodsOptionVO giftGoods : giftGoodsList) {
                                    CouponsGiftGoodsBO couponsGiftGoodsBO = new CouponsGiftGoodsBO();
                                    couponsGiftGoodsBO.setShopGoodsId(giftGoods.getShopGoodsId());
                                    couponsGiftGoodsBO.setShopGoodsOptionId(giftGoods.getShopGoodsOptionId());
                                    couponsGiftGoodsBO.setNum(1);
                                    couponsGiftGoodsBO.setShopGoodsName(giftGoods.getShopGoodsName());
                                    couponsGiftGoodsBO.setSpec(giftGoods.getOptionTitle());
                                    couponsGiftGoodsBO.setThumb(giftGoods.getOptionThumb());
                                    couponsGiftGoodsBO.setIsPost(giftGoods.getIsPost());
                                    couponsGiftGoodsBO.setDeliveryTemplateId(giftGoods.getDeliveryTemplateId());
                                    couponsGiftGoodsBO.setPrice(giftGoods.getOptionPrice());
                                    couponsGiftGoodsBO.setTotalPrice(0);
                                    couponsGiftGoodsBO.setPayment(0);
                                    couponsGiftGoodsBO.setCouponsPrice(0);
                                    couponsGiftGoodsBO.setActivityCouponsGiftsId(giftGoods.getActivityCouponsGiftsId());
                                    couponsGiftGoodsBOList.add(couponsGiftGoodsBO);
                                }
                            }
                            couponsBO.setGiftGoodsList(couponsGiftGoodsBOList);
                            this.fullReduceGiftCouponsBO = couponsBO;
                            break;
                        case Goods:
                            GoodsOptionBO goods = couponsService.getViewModel("get_goods_and_option",
                                    GoodsOptionBO.class, new Object[]{fullReduce.getShopGoodsId(),fullReduce.getShopGoodsOptionId(),fullReduce.getComId()});
                            if(null == goods){
                                throw new ServiceException(ShopGoodsCode.GoodsNotExist);
                            }
                            FullReduceGiftGoodsBO fullReduceGiftGoodsBO = new FullReduceGiftGoodsBO();
                            fullReduceGiftGoodsBO.setShopGoodsId(goods.getShopGoodsId());
                            fullReduceGiftGoodsBO.setShopGoodsOptionId(goods.getShopGoodsOptionId());
                            fullReduceGiftGoodsBO.setNum(1);
                            fullReduceGiftGoodsBO.setShopGoodsName(goods.getShopGoodsName());
                            fullReduceGiftGoodsBO.setSpec(goods.getOptionTitle());
                            fullReduceGiftGoodsBO.setThumb(goods.getOptionThumb());
                            fullReduceGiftGoodsBO.setIsPost(goods.getIsPost());
                            fullReduceGiftGoodsBO.setDeliveryTemplateId(goods.getDeliveryTemplateId());
                            fullReduceGiftGoodsBO.setPrice(goods.getOptionPrice());
                            fullReduceGiftGoodsBO.setTotalPrice(0);
                            fullReduceGiftGoodsBO.setPayment(0);
                            fullReduceGiftGoodsBO.setActivityTitle("满"+fullReduce.getFullPrice()+"赠礼品");
                            fullReduceGiftGoodsBO.setEndDate(this.activityBO.getEndDate());
                            fullReduceGiftGoodsBO.setMarketActivityId(this.activityBO.getMarketActivityId());
                            this.fullReduceGiftGoodsBO = fullReduceGiftGoodsBO;
                            break;
                        case Free:
                            goodsBO.setIsPost(YesOrNo.Yes.name());
                            break;
                    }

                    fee = NumberUtil.add(fee.doubleValue(),NumberUtil.sub(goodsBO.getTotalPrice(),totalPrice)).intValue();
                    goodsBO.setActivityPrice(activityPrice);
                    goodsBO.setTotalPrice(totalPrice);
                    goodsBO.setPayment(totalPrice);
                }
                break;
            }
        }


        this.activityGoodsList = activityGoodsList;
        this.fee = fee;

        return isMeet;
    }

    /**
     * 返回参与活动的商品列表及活动价格
     *
     * @return
     */
    @Override
    public List<GoodsBO> getActivityGoods() {
        return this.activityGoodsList;
    }

    /**
     * 返回赠品列表（满赠活动专用）
     *
     * @return
     */
    @Override
    public FullReduceGiftGoodsBO getGiftGoods() {
        return this.fullReduceGiftGoodsBO;
    }

    /**
     * 返回赠优惠券列表（满赠活动专用）
     *
     * @return
     */
    @Override
    public CouponsBO getGiftCoupons() {
        return this.fullReduceGiftCouponsBO;
    }

    /**
     * 返回优惠收费说明项
     *
     * @return
     */
    @Override
    public Integer getFee() {
        return this.fee;
    }
}
